using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;

namespace Tomahawk.Runtime.Input
{
    //events when all patterns have been tested in all players
    public delegate void BeforePadsUpdate(float elapsedTime);
    public delegate void AfterPadsUpdate(float elapsedTime);

    public class GamePadsManager
    {
        private ContentManager xmlContentManager;
        public ContentManager XmlContentManager
        {
            get { return xmlContentManager; }
        }

        #region "Private members: players"

        private Dictionary<PlayerIndex, GamePadManager> players;
        
        public event BeforePadsUpdate OnBeforePadsUpdate;
        public event AfterPadsUpdate OnAfterPadsUpdate;

        #endregion

        #region "Getters & setters"

        public Dictionary<PlayerIndex, GamePadManager> Players { get { return players; } }
        public GamePadManager PlayerOne { get { return players[PlayerIndex.One]; } }
        public GamePadManager PlayerTwo { get { return players[PlayerIndex.Two]; } }
        public GamePadManager PlayerThree { get { return players[PlayerIndex.Three]; } }
        public GamePadManager PlayerFour { get { return players[PlayerIndex.Four]; } }
        
        #endregion

        public GamePadsManager()
        {
            //create a content manager for XML patterns loading
            xmlContentManager = new ContentManager(Engine.Instance.Services);

            players = new Dictionary<PlayerIndex, GamePadManager>();
            
            GamePadManager playerOne = new GamePadManager(PlayerIndex.One);
            players.Add(PlayerIndex.One, playerOne);

            GamePadManager playerTwo = new GamePadManager(PlayerIndex.Two);
            players.Add(PlayerIndex.Two, playerTwo);

            GamePadManager playerThree = new GamePadManager(PlayerIndex.Three);
            players.Add(PlayerIndex.Three, playerThree);

            GamePadManager playerFour = new GamePadManager(PlayerIndex.Four);
            players.Add(PlayerIndex.Four, playerFour);
        }

        float elapsedAcumTime = 0;

        public void Update(float elapsedTime)
        {
            elapsedAcumTime += elapsedTime;

            foreach (KeyValuePair<PlayerIndex, GamePadManager> player in players)
            {
                player.Value.Update(elapsedTime);
            }

            if (OnBeforePadsUpdate != null) OnBeforePadsUpdate(elapsedTime);            
    
            #region "update stuff"

            //all update stuff
            
            //matching patterns
            foreach (KeyValuePair<PlayerIndex, GamePadManager> player in players)
            {
                player.Value.Patterns.Match(player.Value.State, elapsedAcumTime);
            }

            #endregion

            if (OnAfterPadsUpdate != null) OnAfterPadsUpdate(elapsedTime);
        }

    }

}